package fr.cnam.projetSiw.client.structure.pages.suggest;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.cnam.projetSiw.client.core.ContextKeeper;
import fr.cnam.projetSiw.client.ressources.GeneralBundle;
import fr.cnam.projetSiw.client.structure.DescArtistPlace;
import fr.cnam.projetSiw.shared.Artist;
import fr.cnam.projetSiw.widget.PhotoSquare;

public class TopArtistViewImpl extends VerticalPanel implements TopArtistView
{

	private Label lblTitleDeb = new Label("Top des ");
	private Label lblTitleStrong = new Label(" artistes ");
	private Label lblTitleFin = new Label(" dans votre ville ");

	public TopArtistViewImpl()
	{
		super();
		addStyleName(GeneralBundle.INSTANCE.css().tableSquare());
		lblTitleDeb.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleStrong.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleFin.addStyleName(GeneralBundle.INSTANCE.css().titleSquare());
		lblTitleStrong.addStyleName(GeneralBundle.INSTANCE.css().colorBlue());
		FlowPanel pnl = new FlowPanel();
		pnl.addStyleName(GeneralBundle.INSTANCE.css().inline());
		pnl.add(lblTitleDeb);
		pnl.add(lblTitleStrong);
		pnl.add(lblTitleFin);

		add(pnl);
		this.setCellHeight(pnl, "87px");
	}

	private Presenter presenter;

	@Override
	public void setPresenter(ZicPresenter presenter)
	{
		this.presenter = (Presenter) presenter;

	}

	@Override
	public void setData(ArrayList<Artist> result)
	{
		int cpt = 0;
		for (final Artist artist : result)
		{
			if (cpt == 6)
				break;
			if (artist.getImg() == null || artist.getImg().isEmpty())
				continue;

			// je jarte ceux qui vont me faire un rendu dégueulasse
			if (artist.getImg() == null || artist.getImg().isEmpty() || artist.getGenres() == null || artist.getGenres().isEmpty())
				continue;

			// idem
			int lenght = 0, cptGenre = 0;
			ArrayList<String> tmp = new ArrayList<String>(artist.getGenres());
			for (int i = 0; i < artist.getGenres().size(); i++)
			{
				if (cptGenre == 2)
					break;
				artist.getGenres().set(i, artist.getGenres().get(i).trim());
				if (lenght + artist.getGenres().get(i).length() > 29)
				{
					artist.getGenres().remove(i);
					i--;
				} else
				{
					lenght += artist.getGenres().get(i).length();
					cptGenre++;
				}

			}
			if (artist.getGenres().isEmpty())
			{
				continue;
			}

			PhotoSquare square = new PhotoSquare();

			// String date = "Le " +
			// DateTimeFormat.getFormat("dd MMMM yyyy").format(artist.getBirthDate());

			FocusPanel pnl = square.addSquareArtiste(artist.getImg(), artist.getName(), "Nbr. de vues : " + artist.getPlays(), artist.getGenres());
			artist.setGenres(tmp);
			pnl.addClickHandler(new ClickHandler()
			{

				@Override
				public void onClick(ClickEvent event)
				{
					ContextKeeper.getInstance().setArtist(artist);
					presenter.goTo(new DescArtistPlace("DescArtistePlace"));
				}
			});
			
			add(square);
			cpt++;
		}
	}

}
